package com.jike.noobmoney.mvp.view.activity;

import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.jike.noobmoney.R;
import com.jike.noobmoney.contants.ConstantValue;
import com.jike.noobmoney.mvp.presenter.UserPresenter;
import com.jike.noobmoney.mvp.view.widget.ConfirmTixianDialog;
import com.jike.noobmoney.net.IView;
import com.jike.noobmoney.util.AppUtils;
import com.jike.noobmoney.util.ComUtils;
import com.jike.noobmoney.util.SPUtils;
import com.jike.noobmoney.util.ToastUtils;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 支付宝提现
 * Create by xuke on 2018/11/10
 */
public class TiXianActivity extends BaseActivity implements IView {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.et_tixian_name)
    EditText etTixianName;
    @BindView(R.id.et_tixian_zhanghao)
    EditText etTixianZhanghao;
    @BindView(R.id.et_tixian_money)
    EditText etTixianMoney;
    @BindView(R.id.tv_tixian)
    Button tvTixian;

    private boolean isNameEmpty = true;
    private boolean isZhangEmpty = true;
    private boolean isMoneyEmpty = true;
    private String userid;
    private UserPresenter userPresenter;
    private double ableMoney;


    @Override
    protected int layoutResID() {
        return R.layout.activity_tixian;
    }

    @Override
    protected void initData() {
        tvTitle.setText("支付宝提现");
        Intent intent = getIntent();
        ableMoney = intent.getDoubleExtra(ConstantValue.IntentKey.ABLE_MONEY, 0);

        etTixianName.addTextChangedListener(new TiXianTextWatch(0));
        etTixianZhanghao.addTextChangedListener(new TiXianTextWatch(1));
        etTixianMoney.addTextChangedListener(new TiXianTextWatch(2));

        userid = SPUtils.getInstance().getString(ConstantValue.SpType.userid);
        userPresenter = new UserPresenter(this);
    }


    class TiXianTextWatch implements TextWatcher {

        private int WatchNum;


        public TiXianTextWatch(int num) {
            WatchNum = num;
        }

        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            if (WatchNum == 0) {
                isNameEmpty = charSequence.length() <= 0;
            }

            if (WatchNum == 1) {
                isZhangEmpty = charSequence.length() <= 0;
            }

            if (WatchNum == 2) {
                isMoneyEmpty = charSequence.length() <= 0;
            }

            if (!isNameEmpty && !isZhangEmpty && !isMoneyEmpty) {
                tvTixian.setSelected(true);
            } else {
                tvTixian.setSelected(false);
            }
        }

        @Override
        public void afterTextChanged(Editable editable) {

        }
    }

    @OnClick({R.id.iv_back, R.id.tv_tixian})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_tixian:
                if (tvTixian.isSelected()) {
                    if (AppUtils.isFastClick()) {
                        ToastUtils.showShortToastSafe("操作有点频繁,稍后重试");
                        return;
                    }
//                    String lastTime = SPUtils.getInstance().getString(ConstantValue.SpType.day, "2019-03-11");
//                    String nowTime = ComUtils.getDay();
//                    if (nowTime.equals(lastTime)) {
//                        ToastUtils.showShortToastSafe("每天只能提现一次,明天再来吧");
//                        return;
//                    }

//                    int anInt = SPUtils.getInstance().getInt(ConstantValue.SpType.cishu, 0);
//                    if (anInt > 20) {
//                        ToastUtils.showShortToastSafe("每天只能提现20次,明天再来吧");
//                        return;
//                    }

                    String name = etTixianName.getText().toString().trim();
                    String zhanghao = etTixianZhanghao.getText().toString().trim();
                    String money = etTixianMoney.getText().toString().trim();

                    if (Double.valueOf(money) / 108 > ableMoney) {
                        ToastUtils.showShortToastSafe("可用余额不足");
                        return;
                    }

                    if (Double.valueOf(money) / 108 > 100) {
                        ToastUtils.showShortToastSafe("单次提现不能超过100元");
                        return;
                    }

                    ConfirmTixianDialog instance = ConfirmTixianDialog.getInstance(name, zhanghao, money);
                    instance.show(getSupportFragmentManager(), "confirmtixian");
                    instance.setClickCallback(new ConfirmTixianDialog.OnClickCallback() {
                        @Override
                        public void cancel() {
                            instance.dismiss();
                        }

                        @Override
                        public void confirm() {
                            instance.dismiss();
                            tixian();
                        }
                    });

                }
                break;
        }
    }

    /**
     * 提现
     */
    private void tixian() {
        //提现,保存提现的时间
        //SPUtils.getInstance().put(ConstantValue.SpType.day, ComUtils.getDay());
        int anInt = SPUtils.getInstance().getInt(ConstantValue.SpType.cishu, 0);
        SPUtils.getInstance().put(ConstantValue.SpType.cishu, anInt + 1);

        String name = etTixianName.getText().toString().trim();
        String zhanghao = etTixianZhanghao.getText().toString().trim();
        String money = etTixianMoney.getText().toString().trim();

        showProgress();
       // userPresenter.tixian(userid, zhanghao, name, money, ConstantValue.RequestKey.tixian);

    }

    @Override
    public void onSuccess(String code, String msg, Object data, String actionType) {
        if (ConstantValue.RequestKey.tixian.equals(actionType)) {
            dismissProgress();
            ToastUtils.showShortToastSafe(msg);
            setResult(RESULT_OK);
            finish();
        }
    }

    @Override
    public void onFailed(String code, String msg) {
        dismissProgress();
        ToastUtils.showShortToastSafe(msg);
    }
}
